package com.sdry.web.controller.lz;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sdry.model.lz.InventoryDetail;
import com.sdry.model.lz.InventoryOrder;
import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.Warehouse;
import com.sdry.model.zc.ZcSysUserEntity;
import com.sdry.service.lz.InventoryService;
import com.sdry.utils.ResponseUtil;

/**
 * @ClassName InventoryController
 * @Description 盘点管理
 * @Author lz
 * @Date 2019年3月26日 14:17:05
 * @Version 1.0
 */
@Controller
@RequestMapping("/inventory")
public class InventoryController {

	public static final Integer AFFECT_ROW = 0;
	
	@Resource
	InventoryService inventoryService;
	
	/**
	 * 新增盘点单
	 * @param response
	 * @param inventoryOrder
	 */
	@RequestMapping("/addInventoryOrder")
	public void addInventoryOrder(HttpServletResponse response, InventoryOrder inventoryOrder, HttpSession session){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		inventoryOrder.setInventory_time(sdf.format(new Date()));
		ZcSysUserEntity user = (ZcSysUserEntity)session.getAttribute("user");
		inventoryOrder.setInventory_man(user.getId());
		Long affectRow = inventoryService.addInventoryOrder(inventoryOrder);
		try {
			ResponseUtil.write(response, affectRow);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 新增盘点单详细
	 * @param response
	 * @param detail
	 */
	@RequestMapping("/addInventoryOrderDetail")
	public void addInventoryOrderDetail(HttpServletResponse response, InventoryDetail detail){
		Long affectRow = inventoryService.addInventoryOrderDetail(detail);
		try {
			ResponseUtil.write(response, affectRow);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

 	/**
 	 * 根据ID删除该行数据
 	 * @param response
 	 * @param idArr
 	 */
 	@RequestMapping("/deleteInventoryOrderById")
	public void deleteInventoryOrderById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr){
 		if(idArr.length > 0){
 			for (int i = 0; i < idArr.length; i++) {
 	            Long affectRow = inventoryService.deleteInventoryOrderById(Long.parseLong(idArr[i]));
 	            if (affectRow > AFFECT_ROW) {
 	                try {
						ResponseUtil.write(response, affectRow);
					} catch (Exception e) {
						e.printStackTrace();
					}
 	            } else {
 	                try {
						ResponseUtil.write(response, AFFECT_ROW);
					} catch (Exception e) {
						e.printStackTrace();
					}
 	            }
 	        }
 		}
	}
	
 	/**
 	 * 根据盘点单详细ID删除订单信息
 	 * @param response
 	 * @param id 订单详细ID
 	 */
 	@RequestMapping("/deleteInventoryOrderDetailById")
	public void deleteInventoryOrderDetailById(HttpServletResponse response, String id){
 		Long affectRow = inventoryService.deleteInventoryOrderDetailById(Long.parseLong(id));
 		if (affectRow > AFFECT_ROW) {
 			try {
				ResponseUtil.write(response, affectRow);
			} catch (Exception e) {
				e.printStackTrace();
			}
 		} else {
 			try {
				ResponseUtil.write(response, "");
			} catch (Exception e) {
				e.printStackTrace();
			}
 		}
	}
 	
 	/**
 	 * 行内编辑盘点订单表
 	 * @param response
 	 * @param id 订单ID
 	 * @param fieldName 修改的字段名称
 	 * @param fieldValue 修改的字段的值
 	 */
 	@RequestMapping("/updateInventoryOrderById")
	public void updateInventoryOrderById(HttpServletResponse response, String id, String fieldName, String fieldValue){
		Long affectRow = inventoryService.updateInventoryOrderById(id, fieldName, fieldValue);
		if(affectRow > AFFECT_ROW){
			try {
				ResponseUtil.write(response, affectRow);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				ResponseUtil.write(response, "");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
 	 * 行内编辑盘点订单表详细
 	 * @param response
 	 * @param id 订单ID
 	 * @param fieldName 修改的字段名称
 	 * @param fieldValue 修改的字段的值
 	 */
 	@RequestMapping("/updateInventoryOrderDetailById")
	public void updateInventoryOrderDetailById(HttpServletResponse response, String mid, String fieldName, String fieldValue,String cid,String detailid){
		//System.out.println(mid+"**********gys"+cid);
 		Long affectRow = inventoryService.updateInventoryOrderDetailById(mid, fieldName, fieldValue,cid,detailid);
		if(affectRow > AFFECT_ROW){
			try {
				ResponseUtil.write(response, affectRow);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			try {
				ResponseUtil.write(response, "");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
 	
	/**
	 * 分页条件查询盘点单
	 * @param response
	 * @param criteria
	 */
	@RequestMapping("/queryInventoryOrderCriteria")
	public void queryInventoryOrderCriteria(HttpServletResponse response, LzQueryCriteria criteria){
		String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<InventoryOrder> inventoryOrderList = inventoryService.queryInventoryOrderCriteria(criteria);
        int count = inventoryService.countInventoryOrderCriteria(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(inventoryOrderList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
	
	/**
 	 * 根据订单编号查询订单详细
 	 * @param response
 	 * @param criteria 订单ID
 	 * @throws Exception
 	 */
 	@RequestMapping("/queryInventoryDetailCriteriaById")
	public void queryInventoryDetailCriteriaById(HttpServletResponse response, LzQueryCriteria criteria) throws Exception{
 		String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<InventoryDetail> inventoryDetailList = inventoryService.queryInventoryDetailCriteriaById(criteria);
        int count = inventoryService.countInventoryDetailCriteriaById(criteria);
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(inventoryDetailList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
 	}
 	
 	/**
	 * 分页条件查询盘点单(Android)
	 * @param response
	 * @param criteria
	 */
	@RequestMapping("/queryInventoryOrderCriteriaAndroid")
	public void queryInventoryOrderCriteriaAndroid(HttpServletResponse response){
        List<InventoryOrder> inventoryOrderList = inventoryService.queryInventoryOrderCriteriaAndroid();
        JSONObject jsonObject = new JSONObject();
        if(inventoryOrderList.size() > AFFECT_ROW){
            jsonObject.put("status", "1");
            jsonObject.put("data", inventoryOrderList);
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "查询数据为空");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
	
	/**
 	 * 根据盘点单号查询盘点详细(Android)
 	 * @param response
 	 * @param criteria 订单ID
 	 * @throws Exception
 	 */
 	@RequestMapping("/queryInventoryDetailCriteriaByIdAndroid")
	public void queryInventoryDetailCriteriaByIdAndroid(HttpServletResponse response, String order_id){
        List<InventoryDetail> inventoryDetailList = inventoryService.queryInventoryDetailCriteriaByIdAndroid(order_id);
        JSONObject jsonObject = new JSONObject();
        if(inventoryDetailList.size() > AFFECT_ROW){
            jsonObject.put("status", "1");
            jsonObject.put("data", inventoryDetailList);
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "查询数据为空");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
 	}
 	
 	/**
 	 * 修改盘点单详细(Android)
 	 * @param response
 	 * @param jsonValue 接收安卓参数
 	 * @throws Exception
 	 */
 	@RequestMapping("/updateInventoryDetailAndroid")
	public void updateInventoryDetailAndroid(HttpServletResponse response, String jsonValue) {
 		Gson gson = new Gson();
        List<InventoryDetail> inventoryDetailList = gson.fromJson(jsonValue,
                new TypeToken<List<InventoryDetail>>() {
                }.getType());
 		JSONObject jsonObject = new JSONObject();
 		if(inventoryDetailList.size() > AFFECT_ROW){
        	for (int i = 0; i < inventoryDetailList.size(); i++) {
        		Long affectRow = inventoryService.updateInventoryDetailAndroid(inventoryDetailList.get(i));
        		if(affectRow > AFFECT_ROW){
        			try {
        				jsonObject.put("status", "1");
        	            jsonObject.put("data", "修改成功");
						ResponseUtil.write(response, jsonObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
        		} else {
        			try {
        				jsonObject.put("status", "0");
        	            jsonObject.put("message", "修改失败");
						ResponseUtil.write(response, jsonObject);
					} catch (Exception e) {
						e.printStackTrace();
					}
        		}
			}
        } else {
        	//System.out.println("盘点单详细为空");
        }
 	}
 	
 	/**
	 * 查询所有仓库
     * @throws Exception 
	 */
	@RequestMapping("/queryAllWarehouseAndroid")
	public void queryAllWarehouseAndroid(HttpServletResponse response) throws Exception{
		List<Warehouse> warehouseList = inventoryService.queryAllWarehouseAndroid();
		JSONObject jsonObject = new JSONObject();
        if(warehouseList.size() > AFFECT_ROW){
            jsonObject.put("status", "1");
            jsonObject.put("data", warehouseList);
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        } else {
        	jsonObject.put("status", "0");
            jsonObject.put("message", "查询数据为空");
            try {
				ResponseUtil.write(response, jsonObject);
			} catch (Exception e) {
				e.printStackTrace();
			}
        }
	}
	
	/**
 	 * 根据盘点单号查询盘点详情
 	 * @param orderId
 	 * @param response
 	 */
	@RequestMapping("/queryInventoryDetailByOrder")
 	public void queryInventoryDetailByOrder(String orderId,HttpServletResponse response) {
		 List<InventoryDetail> inventoryDetailList = inventoryService.queryInventoryDetailByOrder(orderId);

	        JSONArray jsonArray = JSONArray.fromObject(inventoryDetailList);
	        JSONObject jsonObject = new JSONObject();
	        jsonObject.put("code", AFFECT_ROW);
		     jsonObject.put("data", jsonArray);
		    
		        try {
		            ResponseUtil.write(response, jsonObject);
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
	}
 	
	
	
	/**
 	 * 根据盘点详情单号查询盘点详情
 	 * @param id
 	 * @param response
 	 */
	@RequestMapping("/queryInventoryDetailById")
 	public void queryInventoryDetailById(String id,HttpServletResponse response) {
		 InventoryDetail inventoryDetail = inventoryService.queryInventoryDetailById(Long.parseLong(id));
	     JSONArray jsonArray = JSONArray.fromObject(inventoryDetail);
	     JSONObject jsonObject = new JSONObject();
	     jsonObject.put("data", jsonArray);
	        try {
	            ResponseUtil.write(response, jsonObject);
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	}
}
